package org.jeecg.modules.workflow.convert.definition;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import org.jeecg.common.util.json.JsonUtils;
import org.jeecg.common.workflow.vo.definition.form.BpmFormCreateReqVO;
import org.jeecg.common.workflow.vo.definition.form.BpmFormRespVO;
import org.jeecg.common.workflow.vo.definition.form.BpmFormSimpleRespVO;
import org.jeecg.common.workflow.vo.definition.form.BpmFormUpdateReqVO;
import org.jeecg.modules.workflow.entity.BpmForm;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BpmFormConvertImpl implements BpmFormConvert {

    private static final TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};

    @Override
    public BpmForm convert(BpmFormCreateReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmForm bpmForm = new BpmForm();
        bpmForm.setName( bean.getName() );
        bpmForm.setStatus( bean.getStatus() );
        bpmForm.setConf( bean.getConf() );
        List<String> list = bean.getFields();
        bpmForm.setFields( toJson(list));
        bpmForm.setRemark( bean.getRemark() );
        return bpmForm;
    }

    @Override
    public BpmForm convert(BpmFormUpdateReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmForm bpmForm = new BpmForm();

        bpmForm.setId( bean.getId() );
        bpmForm.setName( bean.getName() );
        bpmForm.setStatus( bean.getStatus() );
        bpmForm.setConf( bean.getConf() );
        List<String> list = bean.getFields();
        bpmForm.setFields( toJson(list));
        bpmForm.setRemark( bean.getRemark() );

        return bpmForm;
    }

    @Override
    public BpmFormRespVO convert(BpmForm bean) {
        if ( bean == null ) {
            return null;
        }

        BpmFormRespVO bpmFormRespVO = new BpmFormRespVO();

        bpmFormRespVO.setName( bean.getName() );
        bpmFormRespVO.setStatus( bean.getStatus() );
        bpmFormRespVO.setRemark( bean.getRemark() );
        bpmFormRespVO.setId( bean.getId() );
        bpmFormRespVO.setConf( bean.getConf() );
        List<String> list = JsonUtils.parseObject(bean.getFields(),typeReference);
        if ( list != null ) {
            bpmFormRespVO.setFields( new ArrayList<String>( list ) );
        }
        bpmFormRespVO.setCreateTime( bean.getCreateTime() );

        return bpmFormRespVO;
    }

    @Override
    public List<BpmFormSimpleRespVO> convertList2(List<BpmForm> list) {
        if ( list == null ) {
            return null;
        }

        List<BpmFormSimpleRespVO> list1 = new ArrayList<BpmFormSimpleRespVO>( list.size() );
        for ( BpmForm bpmForm : list ) {
            list1.add( bpmFormToBpmFormSimpleRespVO( bpmForm ) );
        }

        return list1;
    }

    @Override
    public IPage<BpmFormRespVO> convertPage(IPage<BpmForm> page) {
        if ( page == null ) {
            return null;
        }

        IPage<BpmFormRespVO> pageResult = new Page<>();

        pageResult.setRecords(bpmFormListToBpmFormRespVOList(page.getRecords()));
        pageResult.setTotal( page.getTotal() );

        return pageResult;
    }

    protected BpmFormSimpleRespVO bpmFormToBpmFormSimpleRespVO(BpmForm bpmForm) {
        if ( bpmForm == null ) {
            return null;
        }

        BpmFormSimpleRespVO bpmFormSimpleRespVO = new BpmFormSimpleRespVO();

        bpmFormSimpleRespVO.setId( bpmForm.getId() );
        bpmFormSimpleRespVO.setName( bpmForm.getName() );

        return bpmFormSimpleRespVO;
    }

    protected List<BpmFormRespVO> bpmFormListToBpmFormRespVOList(List<BpmForm> list) {
        if ( list == null ) {
            return null;
        }

        List<BpmFormRespVO> list1 = new ArrayList<BpmFormRespVO>( list.size() );
        for ( BpmForm bpmForm : list ) {
            list1.add( convert( bpmForm ) );
        }

        return list1;
    }

    private String toJson(List<String> list){
        String result = null;
        if ( list != null ) {
            result = JsonUtils.toJsonString(list);

        }
        return result;
    }

}
